//
//  DownTableViewCell.m
//  VPPublicUntilitisForPod
//
//  Created by verne on 2017/3/20.
//  Copyright © 2017年 vernepung. All rights reserved.
//

#import "DownTableViewCell.h"
#import "VPHTTPRequestOperationManager.h"
#import "DownloadModel.h"
@interface DownTableViewCell(){
}
@property (weak, nonatomic) IBOutlet UILabel *txtTitleLabel;
@property (weak, nonatomic) IBOutlet UILabel *txtReadLabel;
@property (weak, nonatomic) IBOutlet UIButton *btnCommand;

@end
@implementation DownTableViewCell

- (void)awakeFromNib {
    [super awakeFromNib];
    [self.btnCommand addTarget:self action:@selector(btnCommandClicked) forControlEvents:UIControlEventTouchUpInside];
    // Initialization code
}

- (void)fillCellWithObject:(id)object{
    if (self.currentModel && [self.currentModel isEqual:object])
        return;
    self.currentModel = object;
    self.txtTitleLabel.text = self.currentModel.title;
}

- (void)btnCommandClicked{
    
    switch (self.currentModel.downloadState) {
        case kVPDownloadStateNormal:{
            [[VPHTTPRequestOperationManager manager] startNewDownloadWithModel:self.currentModel];
            break;
        }
        case kVPDownloadStateDownloading:{
            [self.btnCommand setTitle:@"下载中" forState:0];
            [[VPHTTPRequestOperationManager manager] pauseDownloadModel:self.currentModel];
            break;
        }
        case kVPDownloadStateWaiting:
        {
            [[VPHTTPRequestOperationManager manager] cancleWithDownloadModel:self.currentModel];
            [self.btnCommand setTitle:@"等待" forState:0];
            break;
        }
        case kVPDownloadStatePause:{
            [[VPHTTPRequestOperationManager manager] startNewDownloadWithModel:self.currentModel];
            [self.btnCommand setTitle:@"暂停" forState:0];
            break;
        }
        case kVPDownloadStateCompleted:{
            [self.btnCommand setTitle:@"完成" forState:0];
            break;
        }
        case kVPDownloadStateReload:
        case kVPDownloadStateError:
        {
            [[VPHTTPRequestOperationManager manager] startNewDownloadWithModel:self.currentModel];
            [self.btnCommand setTitle:@"错误" forState:0];
            break;
        }
        default:{
            [[VPHTTPRequestOperationManager manager] startNewDownloadWithModel:self.currentModel];
            [self.btnCommand setTitle:@"错误" forState:0];
            break;
        }
    }
}

- (void)readedBytesUpdated
{
    self.txtReadLabel.text = [NSString stringWithFormat:@"%@MB/%@MB",[self getSize:self.currentModel.readedBytes],[self getSize:self.currentModel.totalBytes]];
}

- (void)downloadStateChanged
{
    switch (self.currentModel.downloadState) {
        case kVPDownloadStateNormal:{
            [self.btnCommand setTitle:@"无" forState:0];
            break;
        }
        case kVPDownloadStateDownloading:{
            [self.btnCommand setTitle:@"下载中" forState:0];
            break;
        }
        case kVPDownloadStateWaiting:
        {
            [self.btnCommand setTitle:@"等待" forState:0];
            break;
        }
        case kVPDownloadStatePause:{
            [self.btnCommand setTitle:@"暂停" forState:0];
            break;
        }
        case kVPDownloadStateCompleted:{
            [self.btnCommand setTitle:@"完成" forState:0];
            break;
        }
        case kVPDownloadStateReload:
        case kVPDownloadStateError:
        {
            [self.btnCommand setTitle:@"错误" forState:0];
            break;
        }
        default:{
            [self.btnCommand setTitle:@"错误" forState:0];
            break;
        }
    }
}


- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
    
    // Configure the view for the selected state
}

@end
